package com.weather.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weather.domain.TempHumi;
import com.weather.service.TempHumiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/temp-humi")
public class TempHumiController {

    @Autowired
    private TempHumiService tempHumiService;

    /**
     * 分页查询
     */
    @GetMapping("/list")
    public IPage<TempHumi> list(
            @RequestParam(defaultValue = "1") long pageNum,
            @RequestParam(defaultValue = "10") long pageSize
    ) {
        return tempHumiService.page(new Page<>(pageNum, pageSize));
    }

    /**
     * 根据 ID 查询
     */
    @GetMapping("/{id}")
    public TempHumi getById(@PathVariable Long id) {
        return tempHumiService.getById(id);
    }

    /**
     * 添加一条记录
     */
    @PostMapping
    public boolean save(@RequestBody TempHumi tempHumi) {
        return tempHumiService.saveAndCheckTemperature(tempHumi);
    }


    /**
     * 修改一条记录
     */
    @PutMapping
    public boolean update(@RequestBody TempHumi tempHumi) {
        return tempHumiService.updateById(tempHumi);
    }

    /**
     * 删除记录
     */
    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable Long id) {
        return tempHumiService.removeById(id);
    }

    /**
     * 批量删除
     */
    @PostMapping("/deleteBatch")
    public boolean deleteBatch(@RequestBody List<Long> ids) {
        return tempHumiService.removeByIds(ids);
    }
}
